package com.bsg.upm.controller;

 
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.TemplateConfigService;

@Controller
@RequestMapping("/v1.0/monitor/template")
public class MonitorTemplateController {

	@Resource
	private TemplateConfigService templateConfigService;

	@RequestMapping(value = "/event/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String siteId = (String) paramMap.get("site");
			String metric = (String) paramMap.get("metric");

			respJson = templateConfigService.enableEvent(siteId, metric);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/event/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String siteId = (String) paramMap.get("site");
			String metric = (String) paramMap.get("metric");

			respJson = templateConfigService.disableEvent(siteId, metric);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/event/level/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			String metric = (String) paramMap.get("metric");
			String level = (String) paramMap.get("level");

			respJson = templateConfigService.enbleEventLevel(siteId, metric, level);
			response.setStatus(respJson.getStatus());
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/event/level/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String siteId = (String) paramMap.get("site");
			String metric = (String) paramMap.get("metric");
			String level = (String) paramMap.get("level");

			respJson = templateConfigService.disableEventLevel(siteId, metric, level);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/event/update", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			String metric = (String) paramMap.get("metric");
			String level = (String) paramMap.get("level");
			String start = (String) paramMap.get("start");
			String end = (String) paramMap.get("end");

			respJson = templateConfigService.updateEvent(siteId, metric, level, start, end);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/metric/clean/intervaltime", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getMetricCleanDay(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		try {
			String siteId = (String) paramMap.get("site");
			respJson = templateConfigService.getMetricCleanDay(siteId);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}

	@RequestMapping(value = "/metric/clean/intervaltime", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateMetricCleanDay(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			int day = (int) paramMap.get("interval_day");

			respJson = templateConfigService.updateMetricCleanDay(siteId, day);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	
	
	@RequestMapping(value = "/event/clean/intervaltime", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getEventCleanDay(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		try {
			String siteId = (String) paramMap.get("site");
			respJson = templateConfigService.getEventCleanDay(siteId);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}
	
	
	@RequestMapping(value = "/event/clean/intervaltime", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateEventCleanDay(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			int day = (int) paramMap.get("interval_day");

			respJson = templateConfigService.updateEventCleanDay(siteId, day);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/metric_class/intervaltime", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateMetricClassIntervalTime(@RequestBody Map<String, Object> paramMap,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			String metricClass = (String) paramMap.get("metric_class");
			int time = (int) paramMap.get("interval_time");

			respJson = templateConfigService.updateMetricClassIntervalTime(siteId, metricClass, time);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/event/infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listEventTempalte(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			respJson = templateConfigService.getEventTemplate(siteId);
			if (respJson.getStatus() != HttpStatus.SC_OK) {
				response.setStatus(respJson.getStatus());
			}
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/metrics_class/infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listMetricClassTempalte(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {

			String siteId = (String) paramMap.get("site");
			respJson = templateConfigService.getMetricClassTemplate(siteId);
			if (respJson.getStatus() != HttpStatus.SC_OK) {
				response.setStatus(respJson.getStatus());
			}
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
